package com.luozongyao.eduUcenter.controller;


import com.luozongyao.commonutils.JwtUtils;
import com.luozongyao.commonutils.R;
import com.luozongyao.eduUcenter.entity.UcenterMember;
import com.luozongyao.eduUcenter.entity.vo.RegisterVo;
import com.luozongyao.eduUcenter.service.UcenterMemberService;
import com.luozongyao.orderVo.UcenterMemberOrder;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-08-20
 */
@RestController
@RequestMapping("/eduUcenter/ucenterMember")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService ucenterMemberService;

    //登录
    @PostMapping("login")
    public R login(@RequestBody UcenterMember member){
        //调用service方法实现登录
        // 返回token值，使用jwt生成
        String token = ucenterMemberService.login(member);
        return R.ok().data("token",token);
    }


    @ApiOperation(value = "会员注册")
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo){
        ucenterMemberService.register(registerVo);
        return R.ok();
    }

    @ApiOperation(value = "根据token获取登录信息")
    @GetMapping("getLoginInfo")
    public R getLoginInfo(HttpServletRequest request){
//        System.out.println( "11111");
        //调用jwt工具类的方法。根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //查询用户数据库 根据ID获取用户信息
        UcenterMember member = ucenterMemberService.getById(memberId);
        System.out.println(member);
        return R.ok().data("userInfo",member);
//        return R.ok().data("roles","[admin]").data("name",member.getNickname()).data("avatar",member.getAvatar());
    }

    //根据token字符串获取用户信息
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id){

        UcenterMember member = ucenterMemberService.getById(id);
        UcenterMemberOrder memberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,memberOrder);
        return memberOrder;
    }

    //统计某一天的注册人数
    @GetMapping("registerCount/{day}")
    public R registerCount(@PathVariable String day){
       Integer count  =  ucenterMemberService.registerCount(day);
        return R.ok().data("registerCount",count);
    }



}

